'''
@Company: TWL
@Author: xue jian
@Email: xuejian@kanzhun.com
@Date: 2020-01-14 20:55:16
'''
from rediscluster import StrictRedisCluster
import time, json
import redis
redis_nodes = [{'host': '172.21.34.142', 'port': 6479},
                   {'host': '172.21.34.145', 'port': 6479},
                   {'host': '172.21.34.81', 'port': 6479},
                   {'host': '172.21.34.120', 'port': 6479},
                   {'host': '172.21.34.127', 'port': 6479},
                   {'host': '172.21.34.118', 'port': 6479},
                   {'host': '172.21.34.61', 'port': 6479},
                   {'host': '172.21.34.23', 'port': 6479},
                   {'host': '172.21.34.38', 'port': 6479},
                   {'host': '172.21.34.184', 'port': 6479},
                   {'host': '172.21.34.94', 'port': 6479},
                   {'host': '172.21.34.175', 'port': 6479}
                   ]

# redis_nodes = [{'host': '172.18.39.209', 'port': 6479},
#                    {'host': '172.18.39.208', 'port': 6479}
#             ]

try:
    redis_handler = StrictRedisCluster(startup_nodes=redis_nodes, skip_full_coverage_check=True)
    # redis_conn = redis.Redis(host='172.18.39.209', port= 6479)
    # tmp_map = {'a':1, 'b': 2}
    # redis_conn.hmset('1003', tmp_map)
    # redis_conn.expire('1003', 600)
    # print(redis_conn.hget('1000', '89638330'))




    # model_name = 'base_success' + str(int(time.time()))
    # model_name = 'base_deepfm1571924279'
    # model_name = 'no_view14_20200102'
    # print('model_name = ', model_name)
    # model_name_all = model_name + ',success_20200102'
    # redis_handler.setex("boss_predict_online_model_names", 86400*700, model_name_all)
    # layer_f = open('./success_deepfm.net', 'r')
    # layer = json.load(layer_f)
    # layer_f.close()

    # layer = json.dumps(layer)
    # print(type(layer))
    # model_config = {'url': '172.18.39.201:8235', 'layer': layer}
    # redis_handler.hmset(model_name, model_config)
    # redis_handler.expire(model_name, 86400*60)
    # print(redis_handler.hget(model_name, 'url'))



    # layer_f = open('./final_success.net', 'r')
    # layer = json.load(layer_f)
    # layer_f.close()

    # layer = json.dumps(layer)
    # print(type(layer))
    # model_config = {'url': '172.18.39.204:8234', 'layer': layer}
    # redis_handler.hmset(model_name, model_config)
    # redis_handler.expire(model_name, 86400*60)
    # print(redis_handler.hget(model_name, 'url'))


    # layer_f = open('./success_deepfm.net', 'r')
    # layer = json.load(layer_f)
    # layer_f.close()

    # layer = json.dumps(layer)
    # print(type(layer))
    # model_config = {'url': '172.18.39.201:8234', 'layer': layer}
    # redis_handler.hmset(model_name, model_config)
    # redis_handler.expire(model_name, 86400*60)
    # print(redis_handler.get("boss_predict_online_model_names"))
    model_name = "no_view14_20200102"
    print(model_name)
    a = redis_handler.hgetall(model_name)
    print(a)
    layer = a[b'layer']
    # print(layer)
    layer = layer.decode()
    print(layer)
    # print(json.loads(a))
    # layer.replace
    



    # model_name = "test_debug"
    # # model_config = {'url': '172.18.39.201:8234', 'layer': layer}
    # # redis_handler.hmset(model_name, model_config)
    # # redis_handler.expire(model_name, 86400*30)
    # redis_handler.delete(model_name)
    # print(redis_handler.hget(model_name, 'url'))
    # print('connect')
    # tmp_map = {'a':1, 'b': 2}
    # redis_handler.hmset('1003', tmp_map)
    # redis_handler.expire('1003', 600)
    # print(redis_handler.hget('1003', 'a'))
except Exception as e:
    print("Connect Error! msg = ", e)